package video;

import java.awt.Image;
import java.awt.Toolkit;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.io.IOException;
import java.io.InterruptedIOException;
import java.net.DatagramPacket;
import java.net.DatagramSocket;

import javax.swing.ImageIcon;
import javax.swing.JLabel;
import javax.swing.Timer;

import client.Client;
import dialog.ChatDialog;
import others.RTPpacket;
import others.VideoStream;

public class Receiver {
	String VideoFileName; //video file to request to the server
	String peerIp;
	Client client;
	
	//RTP variables:
	//------------------------------------------------------------------------------------------------------------
	byte[] buf; //buffer used to store data received from the server
	DatagramPacket rcvdp; //UDP packet received from the server
	
	DatagramSocket RTPsocket; //socket to be used to send and receive UDP packets
    private Sender sender = null; 
	//------------------------------------------------------------------------------------------------------------

	//------------------------------------------------------------------------------------------------------------

	//Video constants:
	//------------------------------------------------------------------------------------------------------------
	int MJPEG_TYPE = 26; //RTP payload type for MJPEG video
	VideoStream video; //VideoStream object used to access video frames
	int FRAME_PERIOD = 50; //Frame period of the video to stream, in ms
	int VIDEO_LENGTH = 500; //length of the video in frames
	byte[] bufsend; //buffer used to store the images to send to the client
	int STOPED = 1;
	int PLAYED = 2;
	int PAUSED = 3;
	int videostate = STOPED;
	int RTP_RCV_PORT;
	
	Timer timer;
	ImageIcon icon;
	
	ChatDialog chatDialog;
	
    public Receiver(int RTP_RCV_PORT) {
       this.RTP_RCV_PORT = RTP_RCV_PORT;
       timer = new Timer(20, new timerListener());
       timer.setInitialDelay(0);
       timer.setCoalesce(true);
       buf = new byte[15000];

    }

    public void initReceiver() {
	      timer.start();
    }
    
    class timerListener implements ActionListener {
        public void actionPerformed(ActionEvent e) {

         try{
        	 
            RTPsocket = new DatagramSocket(RTP_RCV_PORT);
            RTPsocket.setSoTimeout(5);
          //Construct a DatagramPacket to receive data from the UDP socket
          rcvdp = new DatagramPacket(buf, buf.length);

          System.out.println("ESTA ENTRANDO NO RECEIVER!!!");
    	//receive the DP from the socket:
    	  RTPsocket.receive(rcvdp);

    	//create an RTPpacket object from the DP
    	RTPpacket rtp_packet = new RTPpacket(rcvdp.getData(), rcvdp.getLength());
    	rtp_packet.printheader();
    	//get the payload bitstream from the RTPpacket object
    	int payload_length = rtp_packet.getpayload_length();
    	byte [] payload = new byte[payload_length];
    	rtp_packet.getpayload(payload);

    	//get an Image object from the payload bitstream
    	Toolkit toolkit = Toolkit.getDefaultToolkit();
    	Image image = toolkit.createImage(payload, 0, payload_length);

    	//display the image as an ImageIcon object
    	icon = new ImageIcon(image);
    	chatDialog.iconLabel.setIcon(icon);
    	
          }
          catch (InterruptedIOException iioe){
    	//System.out.println("Nothing to read");
          }
          catch (IOException ioe) {
    	System.out.println("Exception caught: "+ioe);
          }
        }
      }
}
